import { store } from '@/store'

import AuthAPI from '@/api/auth'
import useStoreage from '@vueuse/core'
import { defineStore } from 'pinia'
import {LoginResult} from '@/api/auth'
import {setAccessToken} from '@/utils/auth'

export const useUserStore = defineStore('user', () => {
    function login(data: any) {
        return new Promise<void>((resolve, reject) => {
            AuthAPI.login(data)
                .then((data:LoginResult) => {
                    console.log('login result:', data)
                    setAccessToken(data.access_token)
                    resolve()
                })
                .catch((err) => {
                    reject(err)
                })
        });
    }

    return {
        login
    };
});

export function useUserStoreHook() {
    return useUserStore(store);
  }
  